package com.fenyin.samples.study.jvm.memory;  

import java.lang.reflect.Field;

import sun.misc.Unsafe;
/**
 *VM args:-Xmx20M -XX:MaxDirectMemorySize=10M
 *@Title:  
 *@Description:  直接内存溢出例子
 *@Author:fengzhichao  
 *@Since:2012-8-25  
 *@Version:1.1.0
 */
public class DirectMemoryError {
	private static final int _1MB = 1024*1024;
	
	@SuppressWarnings("restriction")
	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException{
		Field unsafeField = Unsafe.class.getDeclaredFields()[0];
		unsafeField.setAccessible(true);
		Unsafe unsafe = (Unsafe) unsafeField.get(null);
		while(true){
			unsafe.allocateMemory(_1MB);
		}
		
	}
}
